/**
 * 
 */
package com.nashuju.web.common;

import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.durcframework.core.service.CrudService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.nashuju.web.dao.sw.STaskDao;
import com.nashuju.web.entity.sw.STask;
import com.nashuju.web.service.sw.STaskService;

/**
 * @author xiaoren
 *
 */
@Service
public class MyContextListener  extends CrudService<STask, STaskDao> implements ServletContextListener{

	
	@Autowired
	STaskService sTaskService;
	/* (non-Javadoc)
	 * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
	 */
	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	/* (non-Javadoc)
	 * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
	 */
	@Override
	public void contextInitialized(ServletContextEvent arg0) {
		System.out.println("hello my listener");
		showTimer();
		
	}
	
	
	
	 static int count = 0;
	    
	    public void showTimer() {
	        TimerTask task = new TimerTask() {
	            @Override
	            public void run() {
	            	
	            	//sTaskService.getReleaseList();
	                ++count;
	                System.out.println("时间=" + new Date() + " 执行了" + count + "次"); // 1次
	            }
	        };

	        //设置执行时间
	        Calendar calendar = Calendar.getInstance();
	        int year = calendar.get(Calendar.YEAR);
	        int month = calendar.get(Calendar.MONTH);
	        int day = calendar.get(Calendar.DAY_OF_MONTH);//每天
	        //定制每天的21:09:00执行，
	        calendar.set(year, month, day, 14, 29, 00);
	        Date date = calendar.getTime();
	        Timer timer = new Timer();
	        System.out.println(date);
	        
	        int period = 2 * 1000;
	        //每天的date时刻执行task，每隔2秒重复执行
	        timer.schedule(task, date);
	        //每天的date时刻执行task, 仅执行一次
	        //timer.schedule(task, date);
	    }
	
	

}
